package com.yjt.sousou.camera.preview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.create.entity.ImageItem;
import com.yjt.sousou.utils.FixFileCallback;
import com.yjt.sousou.utils.HDFileUtil;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者：83734
 * 时间：2018/11/24
 * 描述：
 */
public class ImagePreviewActivity extends BaseActivity {
    public static final String IMAGE_DATA = "image_data";
    public static final String CURRENT_POSITION = "current_position";
    public static final String FROM = "from";
    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    ArrayList<ImageItem> mImageItems;
    ImagePagerAdapter mPagerAdapter;
    protected int mCurrentPosition = 0;
    private int mFrom;
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_image_preview;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        mCurrentPosition = getIntent().getIntExtra(CURRENT_POSITION, 0);
        mFrom = getIntent().getIntExtra(FROM, 0);
        mImageItems = (ArrayList<ImageItem>) getIntent().getSerializableExtra(IMAGE_DATA);
        mPagerAdapter = new ImagePagerAdapter(this, mImageItems);
        mPagerAdapter.setFrom(mFrom);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(mCurrentPosition, false);

        mPagerAdapter.setListener(new ImagePagerAdapter.PhotoViewClickListener() {

            @Override
            public void OnPhotoTapListener(View view, float v, float v1, ImageItem item,ProgressBar progressBar) {
                if(item.isVideo()){
                    if(item.getVideoUri()!=null){
                        Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
                        intent.putExtra("videoUri", item.getVideoUri().toString());
                        startActivity(intent);
                    }else {
                        progressBar.setVisibility(View.VISIBLE);
                        download(item.getPathUrl(),progressBar);
                    }
                }
            }
        });

    }
    private void download(String url, final ProgressBar progressBar){
        String fileDir = Utils.getApp().getExternalFilesDir("img") + File.separator + "JCamera";
        String fileName = "download_"+url.substring(url.lastIndexOf("/")+1);
        LogUtils.i(fileName);
        if(HDFileUtil.isFileExit(fileDir,fileName)){
            progressBar.setVisibility(View.GONE);
            Uri uri = HDFileUtil.getFileUri(new File(fileDir,fileName),this);
            Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
            intent.putExtra("videoUri", uri.toString());
            startActivity(intent);
        }else {
            OkGo.<File>get(url)
                    .tag(this)
                    .execute(new FixFileCallback(fileDir,fileName) {
                        @Override
                        public void onStart(Request<File, ? extends Request> request) {
                            super.onStart(request);
                            showToast("正在加载视频");
                        }
                        @Override
                        public void onSuccess(Response<File> response) {
                            progressBar.setVisibility(View.GONE);
                            Uri uri = HDFileUtil.getFileUri(response.body(),getApplicationContext());
                            Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
                            intent.putExtra("videoUri", uri.toString());
                            startActivity(intent);
                        }
                        @Override
                        public void onError(Response<File> response) {
                            super.onError(response);
                            showToast("加载失败");
                        }
                    });
        }
    }

}
